/*
 * @Author: moeWang
 * @Date: 2022-03-07 14:35:10
 * @LastEditTime: 2022-03-08 17:50:06
 * @LastEditors: moeWang
 * @Description:
 * @FilePath: \nest-web\src\logical\role\controller\role.controller.ts
 */

import { Body, Controller, Param, Post, UseGuards } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from '@nestjs/passport';
import {
  ApiBody,
  ApiExtraModels,
  ApiOperation,
  ApiSecurity,
  ApiTags,
} from '@nestjs/swagger';
import { ApiQueryBodyRequest } from 'src/decorator/api-request.decorator';
import {
  ApiCommonResponse,
  ApiPaginatedResponse,
} from 'src/decorator/api-response.decorator';
import { BaseListResultModel } from 'src/logical/common/models/BaseListResult.model';
import { BaseResultModel } from 'src/logical/common/models/BaseResult.model';
import { Pagination } from 'src/logical/common/models/Pagination.model';
import { QueryModel } from 'src/logical/common/models/Query.model';
import { RoleModel } from '../model/role.model';
import { RoleDeleteModel } from '../model/roleDelete.model';
import { RoleQueryModel } from '../model/roleQuery.model';
import { RoleService } from '../service/role.service';

@ApiSecurity('Authorization')
@ApiTags('角色权限相关接口')
@ApiExtraModels(RoleModel, RoleQueryModel)
@Controller('/api/role')
export class RoleController {
  constructor(
    private readonly jwtService: JwtService,
    private readonly roleService: RoleService,
  ) {}

  @UseGuards(AuthGuard('jwt'))
  @ApiCommonResponse()
  @ApiOperation({ summary: '保存' })
  @Post('save')
  async save(@Body() roleModel: RoleModel) {
    return this.roleService.save(roleModel);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiPaginatedResponse(RoleModel)
  @ApiBody(ApiQueryBodyRequest('queryModel', RoleQueryModel))
  @ApiOperation({ summary: '查询列表' })
  @Post('queryForList')
  async queryForList(
    @Body() queryModel: QueryModel<RoleQueryModel, Pagination>,
  ): Promise<BaseListResultModel<Array<RoleModel>>> {
    return await this.roleService.queryForList(
      queryModel.query,
      queryModel.pagination,
    );
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '删除' })
  @Post('delete')
  async delete(
    @Body() query: RoleDeleteModel,
  ): Promise<BaseResultModel<Object>> {
    return this.roleService.delete(query.id);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '查询详细' })
  @Post('detail/:id')
  async detail(@Param('id') id: string): Promise<BaseResultModel<RoleModel>> {
    return this.roleService.detail(id);
  }
}
